package com.example.SMSTranslate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.firstcode.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	//public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//tartService(new Intent(this, smsInterceptor.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public void send(View view) {
		EditText editText = (EditText) findViewById(R.id.edit_message);
		String message = editText.getText().toString();
		
        //Check Connectivity
		ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
        	new SendMessage().execute(message);
        } else {
            editText.setText("No network connection available.");
        }
	}
	class SendMessage extends AsyncTask<String, Void, String>{
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet("http://10.0.2.2/temp.php?msg=" + URLEncoder.encode(params[0]));
			try {
			    HttpResponse response = httpclient.execute(httpget);
			    if(response != null) {
			        InputStream inputstream = response.getEntity().getContent();
			        return convertStreamToString(inputstream);
			        //return readIt(inputstream, 160);
			    } else {
			    	return "Unable to complete request";
			    }
			} catch (ClientProtocolException e) {
				return e.toString();
			} catch (IOException e) {
				return e.toString();
			} catch (Exception e) {
			    return e.toString();
			}
		}
		@Override
	    protected void onPostExecute(String result) {
			EditText editText = (EditText) findViewById(R.id.edit_message);
			Typeface hindi = Typeface.createFromAsset(getAssets(), "fonts/DroidHindi.ttf");
			editText.setTypeface(hindi);
			sendSMS(result);
			editText.setText(result);
			Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
			Log.d("msg",result);
	   }
		private String convertStreamToString(InputStream is) {
		    String line = "";
		    StringBuilder total = new StringBuilder();
		    try {
		    	BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
		        while ((line = rd.readLine()) != null) {
		            total.append(line  + " ");
		        }
		    } catch (Exception e) {
		    }
		    return total.toString();
		}
		public void sendSMS(String message) {
		    String phoneNumber = "5554";

		    SmsManager smsManager = SmsManager.getDefault();
		    smsManager.sendTextMessage(phoneNumber, "5556", message, null, null);
		}
	}
}